// 注意
require("../css/home.less")
require("../lib/swiper-bundle.min.css")
let Swiper = require("../lib/swiper-bundle.min.js")
document.ready(function () {
    //引入页脚
    window.$utils.tabbar("home")
    // 获取
    let rankNum = document.querySelector("#rankNum")
    let clockNum = document.querySelector("#clockNum")
    let sportNum = document.querySelector("#sportNum")
    let clockBtn = document.querySelector("#clockBtn")

    // 轮播图
    var mySwiper = new Swiper('.swiper-container', {
        direction: 'horizontal', // 垂直切换选项
        loop: true, // 循环模式选项
        autoplay: {  //自动切换
            delay: 1000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        },
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },

        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },

        // 如果需要滚动条
        scrollbar: {
            el: '.swiper-scrollbar',
        },
    })
    let userId = JSON.parse(localStorage.getItem("user")).userId
    console.log(userId);
    // 获取本地数据
    getHomeData(userId)
    function getHomeData() {
        // 获取数据
        axios.get(window.$utils.url + `/headPageInfo?userId=${userId}`)
            .then(function (res) {
                console.log(res);
                let ret = res.data
                let data = ret.data;
                render(data)
            })
            .catch(function (error) {
                console.log(error);
            })
    }
    let flag = true;
    // 封装渲染数据
    function render(obj) {
        rankNum.textContent = obj.rank
        clockNum.textContent = obj.punchIn
        sportNum.textContent = obj.insigniaNum
        clockBtn.textContent = obj.isPunch == "false" ? "今日打卡" : "已打卡"
        flag=obj.isPunch=="false"?false:true
    }
    // 打卡
    clockBtn.addEventListener("click", function () {
        if (flag) {
            window.$utils.toast("fail", "今日已打卡哦")
        } else {
            axios.get(window.$utils.url + `/clockIn?userId=${userId}`)
            .then(function (res) {
                let ret=res.data
                if(ret.status==0){
                    window.$utils.toast("success", "打卡成功哦")
                    getHomeData()
                }else if(ret.stasus==1){
                    window.$utils.toast("fail", "打卡失败哦")
                }

            })
            .catch(function (error) {
                console.log(error);
            })
            
        }

    })
})